package com.eastshop.index.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.eastshop.category.service.CategorySerive;
import com.eastshop.category.vo.Category;
import com.eastshop.product.service.ProductService;
import com.eastshop.product.vo.Product;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 访问首页的Action
 * @author admin
 *
 */
public class IndexAction extends ActionSupport {
	// 注入商品分类的Service
	private CategorySerive categorySerive;
	
	public void setCategorySerive(CategorySerive categorySerive) {
		this.categorySerive = categorySerive;
	}
	
	// 注入商品的Service
	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	// 访问首页的action
	@Override
	public String execute() throws Exception {
		// 查找所有的分类
		List<Category> cList = categorySerive.find();
		// 将分类存入session中
		ActionContext.getContext().getSession().put("cList", cList);
		
		List<List<Product>> cpList = new ArrayList<List<Product>>();
		// 查找所有分类下的商品
		for (int i = 0; i < cList.size(); i++) {
			Integer cid = cList.get(i).getCid();
			// 根据分类id查找商品
			List<Product> list = productService.findByCid(cid);
			cpList.add(list);
		}
		// 将分类下商品的集合存入值栈
		ActionContext.getContext().getValueStack().set("cpList", cpList);
		return "index";
	}
}
